package com.sinothk.base.version.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.base.version.domain.entity.AppVersion;
import com.sinothk.base.version.service.IAppVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 版本信息
 * @Author: Liangyt
 * @Date: 2022-02-17
 * @Version: V1.0
 */
@Api(tags = "版本信息")
@RestController
@RequestMapping("/comm/appVersion")
@Slf4j
public class AppVersionController extends JeecgController<AppVersion, IAppVersionService> {
    @Autowired
    private IAppVersionService appVersionService;

    /**
     * 分页列表查询
     *
     * @param appVersion
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "版本信息-分页列表查询")
    @ApiOperation(value = "版本信息-分页列表查询", notes = "版本信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AppVersion appVersion,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AppVersion> queryWrapper = new QueryWrapper<>();//QueryGenerator.initQueryWrapper(appVersion, req.getParameterMap());
        queryWrapper.lambda().orderByDesc(AppVersion::getAppNum);
        Page<AppVersion> page = new Page<>(pageNo, pageSize);
        IPage<AppVersion> pageList = appVersionService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param appVersion
     * @return
     */
    @AutoLog(value = "版本信息-添加")
    @ApiOperation(value = "版本信息-添加", notes = "版本信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AppVersion appVersion) {
        appVersionService.save(appVersion);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param appVersion
     * @return
     */
    @AutoLog(value = "版本信息-编辑")
    @ApiOperation(value = "版本信息-编辑", notes = "版本信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AppVersion appVersion) {
        appVersionService.updateById(appVersion);
        return Result.OK("编辑成功!");
    }

//    /**
//     * 通过id删除
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "版本信息-通过id删除")
//    @ApiOperation(value = "版本信息-通过id删除", notes = "版本信息-通过id删除")
//    @DeleteMapping(value = "/delete")
//    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
//        appVersionService.removeById(id);
//        return Result.OK("删除成功!");
//    }
//
//    /**
//     * 批量删除
//     *
//     * @param ids
//     * @return
//     */
//    @AutoLog(value = "版本信息-批量删除")
//    @ApiOperation(value = "版本信息-批量删除", notes = "版本信息-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
//    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        this.appVersionService.removeByIds(Arrays.asList(ids.split(",")));
//        return Result.OK("批量删除成功!");
//    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "版本信息-通过id查询")
    @ApiOperation(value = "版本信息-通过id查询", notes = "版本信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AppVersion appVersion = appVersionService.getById(id);
        if (appVersion == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(appVersion);
    }

    /**
     * 通过id查询
     *
     * @return
     */
    @AutoLog(value = "版本信息-通过id查询")
    @ApiOperation(value = "版本信息-通过id查询", notes = "版本信息-通过id查询")
    @GetMapping(value = "/queryLatestVersion")
    public Result<?> queryLatestVersion() {
        try {
            AppVersion appVersion = appVersionService.queryLatestVersion();
            return Result.OK(appVersion);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
